﻿#include "file.h"
#include"QFile"
#include "myNotePad.h"
void myNotePad::newActionSlot() {//新建实现
	// 创建新窗口，并设置其为Qt::Window类型，并指定原窗口为其父窗口
	myNotePad* newWindow = new myNotePad;
	//newWindow->setAttribute(Qt::WA_DeleteOnClose, false); // 阻止窗口在关闭时自动删除
	newWindow->show();

	
}

void myNotePad::openActionSlot() {//打开实现
	//test代码：
	QString path = QFileDialog::getOpenFileName(this, "open", "D:\\365\\OneDrive\\桌面\\Sync_learning\\c++", "*");
	if (!path.isEmpty()) {
		QFile file(path);
		if (file.open(QIODevice::ReadOnly)) {
			QString array;
			while (file.atEnd() == false)//判断是否到文件末尾
			{
				//读一行
				array += file.readLine();
			}
			iTextEdit::loadtext(array, *ui.centralWidget->findChild<QTextEdit*>("mainText",Qt::FindChildrenRecursively), this->getViewMode());
		}
	}
	//
	
}

void myNotePad::saveActionSlot() {//保存实现
	if (name.isEmpty())
	{
		name = QFileDialog::getSaveFileName(this, "保存文件");
	}

	if (!name.isEmpty())
	{
		QFile file(name);
		if (file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
		{
			QTextStream stream(&file);
			stream << mainText->toPlainText();
			file.close();
		}
	}

}

//第一次保存是另保存实现
void myNotePad::saveAsActionSlot() {//另存为实现
	QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"));
	if (!fileName.isEmpty()) {
		// 将文本编辑器中的内容保存到文件
		QFile file(fileName);
		if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
			QTextStream out(&file);
			out << mainText->toPlainText();
			file.close();
		}
	}
}

void myNotePad::closeActionSlot() {//关闭实现
	close();
}



